package com.lw.shiro.realm;

import com.lw.system.ProfileResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Set;

//公共realm
public class HrmRealm extends AuthorizingRealm {
    @Override
    public void setName(String name) {
        super.setName( "HrmRealm" );
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }

    //授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        ProfileResult principal = (ProfileResult) principalCollection.getPrimaryPrincipal();
        Set<String> apis = (Set<String>) principal.getRoles().get( "apis" );
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.setStringPermissions( apis );
        return info;
    }

}
